package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.poi.ExcelUtil;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.api.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Operation(description = "获取分类树")
    @RequiresPermissions("product:category:")
    @GetMapping(value = "/treeSelect/{parentId}")
    public AjaxResult getCategoryTree(@PathVariable Long parentId) {
        //调用CategorySerivce中获取分类数据的方法
        List<Category> categoryList = categoryService.getCategoryTree(parentId);
        return AjaxResult.success(categoryList);
    }

    @PostMapping("/export")
    public void exportCategory(HttpServletResponse response) {
        //查询数据库中的数据
        List<Category> list = categoryService.list();
        ExcelUtil<Category> categoryExcelUtil = new ExcelUtil<Category>(Category.class);
        categoryExcelUtil.exportExcel(response, list, "商品分类");
    }

    @PostMapping("/importTemplate")
    public AjaxResult inputCategory(MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<Category> util = new ExcelUtil<>(Category.class);
        List<Category> categoryList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = categoryService.importCategory(categoryList, updateSupport, operName);
        return success(message);
    }

    @InnerAuth
    @Operation(summary = "获取商品一级分类")
    @GetMapping(value = "/getOneCategory/{parentId}")
    public R<List<CategoryVo>> getOneCategory(@PathVariable Long parentId) {
        //调用当前类中获取分类树的方法
        AjaxResult categoryTree = getCategoryTree(parentId);
        List<Category> categoryList = (List<Category>) categoryTree.get("data");
        //使用stream流将List<Category>转换成List<CategoryVo>集合
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return R.ok(categoryVoList);
    }

    @InnerAuth
    @Operation(summary = "获取商品分类树")
    @GetMapping("getCategoryTree")
    public R<List<CategoryVo>> getCategoryTree(){
        //调用categoryService中获取分类树的方法
        List<CategoryVo> categoryVoList = categoryService.getAllCategoryTree();
        return R.ok(categoryVoList);
    }

}
